<?php

namespace app\controllers;

use app\components\AppController;
use app\helpers\AppHelper;
use app\helpers\BookHelper;
use app\helpers\JsHelper;
use app\models\Book;
use app\models\BookItem;
use app\models\Setting;
use app\models\UserFavorite;
use app\models\UserHistory;
use app\models\UserScore;
use Throwable;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\web\NotFoundHttpException;
use yii\web\Response;

class ComicController extends AppController {

    /**
     * @param null $id
     * @return string
     * @throws NotFoundHttpException
     */
    public function actionDetail($id = NULL) {
        $model = Book::findOne($id);
        if (!$model) {
            throw new NotFoundHttpException('漫画未找到');
        }

        $is_favorite = UserFavorite::getFavorite($id);
        $is_score = UserScore::getUserScore($id);
        $this->view->title = $model->title;

        Book::updateAllCounters(['view_count' => 1], ['id' => $id]);

        $this->seo($model->description, $model->title, $model->thumbnail);
        return $this->render(compact('model', 'is_favorite', 'is_score'));
    }


    /**
     * @param $id
     * @param $itemId
     * @return string|Response
     * @throws NotFoundHttpException
     * @throws Throwable
     */
    public function actionItem($id, $itemId) {
        $book = Book::findOne($id);
        if (!$book) {
            throw new NotFoundHttpException();
        }

        $item = BookItem::findOne(['book_id' => $id, 'id' => $itemId]);
        if (!$item) {
            return $this->redirect(['comic/detail', 'id' => $id]);
        }

        $is_favorite = UserFavorite::getFavorite($id);
        $items = $book->items;

        $data = ['id' => $book->id, 't' => time(), 'book_item_name' => BookHelper::getBookItemName($item)];
        $data['book_thumb'] = AppHelper::formatImage($book->thumbnail);
        $data['book_item-url'] = Url::to('', TRUE);
        $data['book_url'] = Url::to(["{$this->id}/detail", 'id' => $book->id]);
        $data['book_title'] = $book->title;
        $data['book_images'] = $item->getContent();
        $data['book_item_sort'] = $item->sort;

        $data = Json::encode($data);
        $js = new JsHelper("window.comic_data = {$data};");
        $this->view->registerJs($js->pack(), 2);
//        $this->view->registerJs("window.comic_data = {$data};", 2);

        $index = array_search($item['id'], array_column($items, 'id'));
        $next_url = isset($items[$index + 1]) ? Url::to(['comic/item', 'id' => $book['id'], 'itemId' => $items[$index + 1]['id']]) : NULL;
        $prev_url = isset($items[$index - 1]) ? Url::to(['comic/item', 'id' => $book['id'], 'itemId' => $items[$index - 1]['id']]) : NULL;

        $this->view->title = "{$book->title} {$item->name}";
        $this->view->params['breadcrumbs'] = [
            ['label' => $book['title'], 'url' => ["{$this->id}/detail", 'id' => $book['id']]],
            ['label' => BookHelper::getFullBookItemName($item), 'url' => 'javascript:void(0);']
        ];
        $this->view->params['book'] = $book;
        $this->view->params['item'] = $item;
        $this->view->params['items'] = $items;
        $this->view->params['next_url'] = $next_url;
        $this->view->params['prev_url'] = $prev_url;
        $this->view->params['is_favorite'] = $is_favorite;

        UserHistory::saveHistory($book, $item);

        $this->seo($book->description, $book->title, $book->thumbnail);
        return $this->render(compact('book', 'item', 'next_url'));
    }

    protected function seo($description, $title, $thumbnail) {
        $name = Setting::get('web_name');
        $this->description = AppHelper::convertText($description);
        $this->keywords = "{$title},{$title}漫画,{$title}画全集,{$title}免费漫画,{$title}漫画下载,漫画大全,好看的漫画,{$name}";
        $this->view->registerMetaTag(['property' => 'og:image', 'content' => AppHelper::formatImage($thumbnail)]);
    }
}